/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.image.demo;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/image")
public class WelcomeController {

	@Value("${application.message:Hello World}")
	private String message = "Hello World";

	@GetMapping("/welcome")
	public String welcome(Map<String, Object> model) {
		model.put("time", new Date());
		model.put("message", this.message);
		return "welcome";
	}

	@RequestMapping("/foo")
	public String foo(Map<String, Object> model) {
		throw new RuntimeException("Foo");
	}
	
	@RequestMapping("/aaa")
	@ResponseBody
	public String aaa(HttpServletRequest req) {
		String aaa = req.getParameter("name");
		System.out.println(aaa + "----aaa");
		return aaa;
	}
	
	@RequestMapping("/index2")
	public String index2(Map<String, Object> model) {
		return "index2";
	}
	
	@RequestMapping("/contact")
	public String contact() {
		return "contact";
	}
	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest req) {
		ModelAndView model = new ModelAndView();
		JSONObject obj = getAllImgs(req);
		model.addObject("data",obj);
		model.setViewName("index");
		return model;
	}

	/**
	 * 获取图片
	 * @param filePath
	 * @param request
	 * @return
	 */
	@RequestMapping("/getImages")
	@ResponseBody
	public List<String> getImages(String filePath,HttpServletRequest request) {
		ArrayList<String> list = new ArrayList<String>();
		String path = request.getServletContext().getRealPath("/")+"static/images/"+filePath;
		System.out.println(path);
		File folder = new File(path);
		File[] listFiles = folder.listFiles();
		for (File file : listFiles) {
			if(checkImg(file.getName())) {
				list.add(file.getName());
			}
		}
		return list;
	}
	
	
	/**
	 * 获取所有图片
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllFiles")
	@ResponseBody
	public JSONObject getAllFiles(HttpServletRequest request) {
		
		return getAllImgs(request);
	}
	
	
	/**
	 * 获取所有图片
	 * @return
	 */
	private JSONObject getAllImgs(HttpServletRequest request) {
		JSONObject obj = new JSONObject();
		
		String[] types =new String[] {"fdmc","ty","tyfspt","tymhgd","tywl","tyxm"};//所有图片类型
		
		for (String type : types) {
			String path = request.getServletContext().getRealPath("/")+"static/images/"+type;
			File folder = new File(path);
			File[] listFiles = folder.listFiles();
			List<String> list = new ArrayList<String>();
			for (File file : listFiles) {
				if(checkImg(file.getName())) {
					list.add(file.getName());
				}
			}
			obj.put(type, list);
		}
		return obj;
	}
	
	/**
	 * 判断文件是不是图片
	 * @param fileName
	 * @return
	 */
	public boolean checkImg(String fileName) {
		if(fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".bmp") || 
				fileName.endsWith(".jpeg") || fileName.endsWith(".tif")) {
			return true;
		}
		return false;
	}
	
	/**
	 * 手机端首页
	 * @param req
	 * @return
	 */
	@RequestMapping("/phone/index")
	public ModelAndView phoneIndex(HttpServletRequest req) {
		ModelAndView model = new ModelAndView();
		JSONObject obj = getAllImgs(req);
		model.addObject("data",obj);
		model.setViewName("phone/index");
		return model;
	}
	
	/**
	 * 手机端联系方式页面
	 * @return
	 */
	@RequestMapping("/phone/contact")
	public String phoneContact() {
		return "phone/contact";
	}
}
